var express = require('express');
var router = express.Router();
var session = require('express-session');
var moment = require('moment');
var async = require('async');


var db = require('./../../utils/db.js');


//获取项目活动列表页面
router.get("/listAll", function (req, res, next) {

            res.render('process/activity/index');
});

/**
 * 根据项目获取活动列表
 */
router.get('/listByActivity', function (req, res, next) {
    var projectId = req.session.projectId;
    var sql = "select * from project_progress_activity as a where a.project_base_id="+ projectId +" ORDER BY a.activity_date DESC";
    db.query(sql, function (err, rows) {
        if (rows && rows.length >= 0) {
            for (var i = 0; i < rows.length; i++) {
                rows[i].activity_date = moment(rows[i].activity_date).format('YYYY-MM-DD HH:mm:ss');
            }
            res.json({
                errcode: 0,
                message: '根据项目获取活动成功',
                data: rows
            });

        } else {
            res.json({
                errcode: 1,
                message: '根据项目获取活动失败'
            });
        }

    });
});

/**
 * 添加
 */
router.get('/add', function (req, res, next) {
    var projectId = req.session.projectId;

    var sql = "select * from project_base as p left join version_base as v on p.id=v.project_base_id where p.id = " + projectId + " ORDER BY v.version_release_time DESC limit 1";
    db.query(sql, function (err, rows) {
        if (rows && rows.length >= 0) {
            res.render('process/activity/edit', {
                id: '',
                activityName: '',
                activityType: '',
                activityDate: '',
                activityEvent: ''
            });
        } else {
            res.render('error', {
                message: err.message,
                error: err
            });
        }

    });
});

/**
 * 编辑
 */
router.get('/edit/:id', function (req, res, next) {
    var id = req.params.id;
    var projectId = req.session.projectId;
    if (id) {
        var sql = "select * from project_base as p left join version_base as v on p.id=v.project_base_id where p.id = " + projectId + " ORDER BY v.version_release_time DESC limit 1";
        var versionSql = "select * from project_progress_activity as a where a.id=" + id + " ORDER BY a.activity_date DESC";
        async.series([
            function (callback) {
                db.query(sql, function (err, rows) {
                    callback(err, rows);
                });
            },
            function (callback) {
                db.query(versionSql, function (err, rows) {
                    callback(err, rows);
                });
            }
        ], function (err, values) {

            if (values && values.length >= 0) {
                for (var i = 0; i < values.length; i++) {
                    for (var j=0; j<i;j++){
                        values[i][j].activity_date = moment(values[i][j].activity_date).format('YYYY-MM-DD HH:mm:ss');

                    }

                }
                res.render('process/activity/edit', {

                    id: values[1][0].id,
                    activityName: values[1][0].activity_name,
                    activityType: values[1][0].activity_type,
                    activityDate: values[1][0].activity_date,
                    activityEvent: values[1][0].event
                });
            } else {
                res.render('process/activity/edit', {
                    id: '',
                    activityName: '',
                    activityType: '',
                    activityDate: '',
                    activityEvent: ''
                });
            }
        });
    }

});


/**
 * 保存
 */
router.post('/save', function (req, res, next) {
    var activityName = req.body.activityName ? req.body.activityName.trim() : '';
    var activityType = req.body.activityType ? req.body.activityType.trim() : '';
    var activityDate = req.body.activityDate ? req.body.activityDate.trim() : '';
    var activityEvent = req.body.activityEvent ? req.body.activityEvent.trim() : '';

    var id = req.body.id;
    var projectId = req.session.projectId;
    var sql = '';
    if (id) {
        sql = "update project_progress_activity set activity_name='" + activityName + "',activity_type='" + activityType + "',activity_date='" + activityDate + "',event='" + activityEvent + "' where id="+ id +"";
    } else {
        sql = "insert into project_progress_activity(activity_name,activity_type,activity_date,event,project_base_id) values ('" + activityName + "','" + activityType + "','" + activityDate + "','" + activityEvent + "','" + projectId + "')";
    }

    db.query(sql, function (err, rows) {
        if (!err) {
            res.redirect("/process/activity/listAll");
        } else {
            res.json({
                errcode: 1,
                message: '保存失败'
            });
        }
    });


});

/**
 * 删除
 */
router.post('/delete/:id', function (req, res, next) {
    var id = req.params.id;
    var sql = '';
    if (id) {
        sql = "delete from project_progress_activity where id=" + id;
        db.query(sql, function (err, rows) {
            if (!err) {
                res.json({
                    errcode: 0,
                    message: '删除成功'
                });
            } else {
                res.json({
                    errcode: 1,
                    message: '删除失败'
                });
            }
        });
    } else {
        res.json({
            errcode: 2,
            message: '系统错误'
        });
    }


});


//活动查询
router.get("/search", function (req, res, next) {
    var activityType = req.query.activityType;
    var projectId = req.session.projectId;
    var sql = "select * from project_base as p left join version_base as v on p.id=v.project_base_id where p.id = " + projectId + " ORDER BY v.version_release_time DESC limit 1";
    if (activityType&&activityType.length > 0){
        var versionSql = "select * from project_progress_activity as a where a.project_base_id=" + projectId +" and activity_type = " + activityType[0] + "";
        if (activityType.length > 1) {
            for (var i = 1; i < activityType.length; i++){
                versionSql += " or activity_type = " + activityType[i] + "" ;
            }
        }
    }else {
        var versionSql = "select * from project_progress_activity as a where a.project_base_id=" + projectId;
    }

    db.query(sql, function (err, rows) {
        if (rows && rows.length > 0) {
            db.query(versionSql, function (err, datas) {
                if (rows && rows.length > 0) {
                    for (var i = 0; i < datas.length; i++) {
                        datas[i].activity_date = moment(datas[i].activity_date).format('YYYY-MM-DD HH:mm:ss');
                    }
                    res.json({
                        errcode: 0,
                        message: '查询成功',
                        data: datas
                    });
                } else {
                    res.json({
                        errcode: 1,
                        message: '查询失败'
                    });
                }
            })
        }
    });
});


module.exports = router;
